package com.example.picture_sharing_system;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.NetworkOnMainThreadException;
import android.provider.SyncStateContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.android.tu.loadingdialog.LoadingDailog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import utils.BaseResponse;
import utils.Constants;
import utils.Works;
import utils.WorksPictureListAdapter;


public class WorksListFragment extends Fragment {


    private GridView gv_works_list;
    private List<Works> worksData;

    private WorksPictureListAdapter worksPictureListAdapter;
    private int userid;

    private LoadingDailog loadingDailog;

    private okhttp3.Callback callback = new okhttp3.Callback() {

        @Override
        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
            if (response.isSuccessful()) {
                final String body = response.body().string();

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        Type jsonType =
                                new TypeToken<BaseResponse<List<Works>>>() {
                                }.getType();
                        BaseResponse<List<Works>> worksBaseResponse =
                                gson.fromJson(body, jsonType);
                        worksData = worksBaseResponse.getData();
                        worksPictureListAdapter = new WorksPictureListAdapter(getContext(), R.layout.works_list_item, worksData);
                        gv_works_list.setAdapter(worksPictureListAdapter);
                        loadingDailog.hide();
                        loadingDailog.dismiss();

                    }
                });
            }
        }

        @Override
        public void onFailure(@NotNull Call call, @NotNull IOException e) {
            e.printStackTrace();
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        loadingDailog = new LoadingDailog.Builder(getContext())
                .setMessage("加载中")
                .setCancelable(true)
                .setCancelOutside(true).create();
        loadingDailog.show();

        View rootView = inflater.inflate(R.layout.fragment_works_list, container, false);
        gv_works_list = rootView.findViewById(R.id.gv_works_list);
        userid = getActivity().getIntent().getIntExtra("userid", 0);

        refreshData();
        return rootView;
    }


    private void refreshData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder()
                        .url(Constants.WORKS_LIST_URL + "?userid=" + userid)
                        .get().build();
                try {
                    OkHttpClient client = new OkHttpClient();
                    client.newCall(request).enqueue(callback);
                } catch (NetworkOnMainThreadException ex) {
                    ex.printStackTrace();
                }
            }
        }).start();
    }
}